﻿/******************************************************/
// AccountPage プレゼンテーションクラス  
/******************************************************/

using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web.UI;
using MAA.Business;
using MAA.Entity;

namespace MAA.Presentation.PRL.Account
{
    public partial class AccountPage : System.Web.UI.Page
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            langNewPassword.Text = rm.GetString("langNewPassword");
            langAccInfo.Text = rm.GetString("langAccInfo");
            langName.Text = rm.GetString("langName");
            RequiredFieldValidator1.Text = rm.GetString("langErrNameEntity");
            langPictureAcc.Text = rm.GetString("langPictureAcc");
            langContact.Text = rm.GetString("langContact");
            RequiredFieldValidator3.Text = rm.GetString("langErrEmailEntity");
            RegularExpressionValidator1.Text = rm.GetString("langErrEmail");
            langPhone.Text = rm.GetString("langPhone");
            langAddress.Text = rm.GetString("langAddress");
            langProvince.Text = rm.GetString("langProvince");
            langRegisterDate.Text = rm.GetString("langRegisterDate");
            langNumOfBeach.Text = rm.GetString("langNumOfBeach");
            langChangePass.Text = rm.GetString("langChangePass");
            langPasslonger6.Text = rm.GetString("langPasslonger6");
            langOldPass.Text = rm.GetString("langOldPass");
            RequiredFieldValidator2.Text = rm.GetString("langOldPass");
            RegularExpressionValidator2.Text = rm.GetString("langPassErr");
            RequiredFieldValidator4.Text = rm.GetString("langOldPass");
            RegularExpressionValidator3.Text = rm.GetString("langPassErr");
            langConfirmNewPass.Text = rm.GetString("langConfirmNewPass");
            RequiredFieldValidator5.Text = rm.GetString("langErrConfirmMailEntity");
            CompareValidator1.Text = rm.GetString("langErrConfirmMail");
            btnSaveChangePass.Text = rm.GetString("langSave");
            btnCancelChangePass.Text = rm.GetString("langCancel");
            btnEdit.Text = rm.GetString("langWarehouseEdit");
            btnSaveEdit.Text = rm.GetString("langWarehouseSave");
            btnCancelEdit.Text = rm.GetString("langWarehouseCancel");
            langEmail.Text = rm.GetString("langEmail");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
            }
            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            tbxName.Text = lblName.Text;
            tbxEmail.Text = lblEmail.Text;
            tbxPhone.Text = lblPhone.Text;
            if (this.IsPostBack == true) return;

            //kiem tra da dang nhap chua
            if (Session["userID"] == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "popup",
                    "<script type='text/javascript'>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_NOT_LOGIN) + "');window.location='../HomePage.aspx'</script>");
            }

            //get userID
            long userID = Convert.ToInt64(Session["userID"]);

            //Hide edit infomation area
            fupProfilePicture.Visible = false;
            tbxName.Visible = false;
            lblValName.Visible = false;
            tbxEmail.Visible = false;
            lblValEmail.Visible = false;
            tbxPhone.Visible = false;
            tbxAddress.Visible = false;
            ddlProvince.Visible = false;
            btnSaveEdit.Visible = false;
            btnCancelEdit.Visible = false;

            // Get province to add to drop down list
            BOLNews bolNews = new BOLNews();
            DataTable tblProvince = new DataTable();
            string messageProvince = "";
            string lang = Session["language"].ToString();
            if (!bolNews.viewProvince(lang,out tblProvince, out messageProvince))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(messageProvince) + "')</script>", false);
            }
            ddlProvince.DataSource = tblProvince;
            ddlProvince.DataValueField = "ID";
            ddlProvince.DataTextField = "Name";
            ddlProvince.DataBind();

            //Load user infomation
            BOLAccount acc = new BOLAccount();
            User user = new User();
            string messageAcc = "";

            if (acc.viewAccInfo(userID, out user, out messageAcc))
            {
                //Show user infomation
                lblName.Text = user.Name;
                impProfilePicture.ImageUrl = user.ProfilePicture;
                lblEmail.Text = user.Email;
                lblPhone.Text = user.Phone;
                lblAddress.Text = user.Address;
                //Set value for provine dropdownlist
                ddlProvince.SelectedValue = user.ProvinceID.ToString();
                lblProvince.Text = ddlProvince.SelectedItem.ToString();
                lblRegisterDate.Text = user.RegisterDate.ToShortDateString();
                lblCurrentViolationNumber.Text = user.ViolationNum.ToString();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                   "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(messageAcc) + "')</script>", false);
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            //Hide infomation area
            lblName.Visible = false;
            lblEmail.Visible = false;
            lblPhone.Visible = false;
            lblAddress.Visible = false;
            lblProvince.Visible = false;
            btnEdit.Visible = false;

            //Get text from label to textbox
            tbxName.Text = lblName.Text;
            tbxEmail.Text = lblEmail.Text;
            tbxPhone.Text = lblPhone.Text;
            tbxAddress.Text = lblAddress.Text;
            ddlProvince.SelectedItem.Text = lblProvince.Text;

            //Show edit infomation area
            fupProfilePicture.Visible = true;
            tbxName.Visible = true;
            lblValName.Visible = true;
            tbxEmail.Visible = true;
            lblValEmail.Visible = true;
            tbxPhone.Visible = true;
            tbxAddress.Visible = true;
            ddlProvince.Visible = true;
            btnSaveEdit.Visible = true;
            btnCancelEdit.Visible = true;
        }

        protected void btnSaveEdit_Click(object sender, EventArgs e)
        {
            //get userID
            long userID = Convert.ToInt64(Session["userID"]);
            //Validate profile picture
            String filePath = "";
            if (fupProfilePicture.HasFile)
            {
                double filesize = (double)fupProfilePicture.FileBytes.Length;
                //Neu file upload > 2MB thi bao loi
                if (filesize > 2097152)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                        "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_LARGE_SIZE_IMAGE) + "')</script>", false);
                    return;
                }

                BOLCommon bolCommon = new BOLCommon();
                //Neu file upload khong phai la anh thi bao loi
                if (BOLCommon.checkFileImage(fupProfilePicture.FileName))
                {
                    string ext = Path.GetExtension(fupProfilePicture.FileName);
                    filePath = Common.URL_PROFILE_IMAGE + userID.ToString() + ext;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                        "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_NOT_BE_IMAGE) + "')</script>", false);
                    return;
                }
            }

            //create user object to change infomation
            User user = new User();
            user.ID = userID;
            user.Name = tbxName.Text.Trim();
            if (filePath.Equals(""))
            {
                user.ProfilePicture = impProfilePicture.ImageUrl;
            }
            else
            {
                user.ProfilePicture = filePath;
            }
            user.Email = tbxEmail.Text.Trim();
            user.Phone = tbxPhone.Text.Trim();
            user.Address = tbxAddress.Text.Trim();
            user.ProvinceID = Convert.ToInt16(ddlProvince.SelectedItem.Value);

            //try to change infomation
            BOLAccount bolAcc = new BOLAccount();
            string message = "";

            if (bolAcc.editAccount(user, out message))
            {
                //change infomation is showed
                lblName.Text = user.Name;
                if (!filePath.Equals(""))
                {
                    //Save image
                    fupProfilePicture.SaveAs(MapPath(filePath));
                    //Set profile picture is picture saved
                    impProfilePicture.ImageUrl = filePath;
                }

                lblEmail.Text = tbxEmail.Text;
                lblPhone.Text = tbxPhone.Text;
                lblAddress.Text = tbxAddress.Text;
                lblProvince.Text = ddlProvince.SelectedItem.ToString();
                //back to show infomation status
                btnCancelEdit_Click(sender, e);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
            }
        }

        protected void btnCancelEdit_Click(object sender, EventArgs e)
        {
            //Hide edit infomation area
            fupProfilePicture.Visible = false;
            tbxName.Visible = false;
            lblValName.Visible = false;
            tbxEmail.Visible = false;
            lblValEmail.Visible = false;
            tbxPhone.Visible = false;
            tbxAddress.Visible = false;
            ddlProvince.Visible = false;
            btnSaveEdit.Visible = false;
            btnCancelEdit.Visible = false;

            //Show infomation area
            lblName.Visible = true;
            lblEmail.Visible = true;
            lblPhone.Visible = true;
            lblAddress.Visible = true;
            lblProvince.Visible = true;
            btnEdit.Visible = true;
        }

        protected void btnSaveChangePass_Click(object sender, EventArgs e)
        {
            string message = "";
            BOLAccount bolAcc = new BOLAccount();
            string oldPass = tbxOldPass.Text.Trim();
            string newPass = tbxNewPass.Text.Trim();

            //get userID
            long userID = Convert.ToInt64(Session["userID"]);

            // Neu doi pass thanh cong thi dong area doi pass
            if (bolAcc.changePassword(userID, oldPass, newPass, out message))
            {
                // 
                ClientScript.RegisterStartupScript(this.GetType(), "popup",
                    "<script type='text/javascript'>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_SUCCESS_CHANGE_PASS) + "');window.location='AccountPage.aspx'</script>");
            }
            else
            {
                // Neu that bai thi show message
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
            }
        }

        protected void btnCancelChangePass_Click(object sender, EventArgs e)
        {
            //reset infomation
            tbxOldPass.Text = "";
            tbxNewPass.Text = "";
            tbxConfirmNewPass.Text = "";
        }
    }
}